﻿/**  
* Ver    变更日期             负责人  变更内容
* V0.01  2015/5/13 16:36:04   何泉水    初版
*/
using System;
using System.Data;
using System.Text;
using System.Data.SQLite;
namespace He.DAL
{
	/// <summary>
	/// 数据访问类:olservice
	/// </summary>
	public partial class olservice
	{
		public olservice()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQLite.GetMaxID("id", "he_olservice"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from he_olservice");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
			parameters[0].Value = id;

			return DbHelperSQLite.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(He.Model.olservice model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into he_olservice(");
			strSql.Append("title,user_id,port,is_red,sort_id,id_type,group_id,pos_mark,attr_str,sttr_str2,attr_num)");
			strSql.Append(" values (");
			strSql.Append("@title,@user_id,@port,@is_red,@sort_id,@id_type,@group_id,@pos_mark,@attr_str,@sttr_str2,@attr_num)");
			strSql.Append(";select LAST_INSERT_ROWID()");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@title", DbType.String),
					new SQLiteParameter("@user_id", DbType.String),
					new SQLiteParameter("@port", DbType.String),
					new SQLiteParameter("@is_red", DbType.Int32,8),
					new SQLiteParameter("@sort_id", DbType.Int32,8),
					new SQLiteParameter("@id_type", DbType.String),
					new SQLiteParameter("@group_id", DbType.Int32,8),
					new SQLiteParameter("@pos_mark", DbType.String),
					new SQLiteParameter("@attr_str", DbType.String),
					new SQLiteParameter("@sttr_str2", DbType.String),
					new SQLiteParameter("@attr_num", DbType.Int32,8)};
			parameters[0].Value = model.title;
			parameters[1].Value = model.user_id;
			parameters[2].Value = model.port;
			parameters[3].Value = model.is_red;
			parameters[4].Value = model.sort_id;
			parameters[5].Value = model.id_type;
			parameters[6].Value = model.group_id;
			parameters[7].Value = model.pos_mark;
			parameters[8].Value = model.attr_str;
			parameters[9].Value = model.sttr_str2;
			parameters[10].Value = model.attr_num;

			object obj = DbHelperSQLite.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(He.Model.olservice model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update he_olservice set ");
			strSql.Append("title=@title,");
			strSql.Append("user_id=@user_id,");
			strSql.Append("port=@port,");
			strSql.Append("is_red=@is_red,");
			strSql.Append("sort_id=@sort_id,");
			strSql.Append("id_type=@id_type,");
			strSql.Append("group_id=@group_id,");
			strSql.Append("pos_mark=@pos_mark,");
			strSql.Append("attr_str=@attr_str,");
			strSql.Append("sttr_str2=@sttr_str2,");
			strSql.Append("attr_num=@attr_num");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@title", DbType.String),
					new SQLiteParameter("@user_id", DbType.String),
					new SQLiteParameter("@port", DbType.String),
					new SQLiteParameter("@is_red", DbType.Int32,8),
					new SQLiteParameter("@sort_id", DbType.Int32,8),
					new SQLiteParameter("@id_type", DbType.String),
					new SQLiteParameter("@group_id", DbType.Int32,8),
					new SQLiteParameter("@pos_mark", DbType.String),
					new SQLiteParameter("@attr_str", DbType.String),
					new SQLiteParameter("@sttr_str2", DbType.String),
					new SQLiteParameter("@attr_num", DbType.Int32,8),
					new SQLiteParameter("@id", DbType.Int32,8)};
			parameters[0].Value = model.title;
			parameters[1].Value = model.user_id;
			parameters[2].Value = model.port;
			parameters[3].Value = model.is_red;
			parameters[4].Value = model.sort_id;
			parameters[5].Value = model.id_type;
			parameters[6].Value = model.group_id;
			parameters[7].Value = model.pos_mark;
			parameters[8].Value = model.attr_str;
			parameters[9].Value = model.sttr_str2;
			parameters[10].Value = model.attr_num;
			parameters[11].Value = model.id;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from he_olservice ");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from he_olservice ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public He.Model.olservice GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,title,user_id,port,is_red,sort_id,id_type,group_id,pos_mark,attr_str,sttr_str2,attr_num from he_olservice ");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
			parameters[0].Value = id;

			He.Model.olservice model=new He.Model.olservice();
			DataSet ds=DbHelperSQLite.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public He.Model.olservice DataRowToModel(DataRow row)
		{
			He.Model.olservice model=new He.Model.olservice();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					model.id=int.Parse(row["id"].ToString());
				}
				if(row["title"]!=null)
				{
					model.title=row["title"].ToString();
				}
				if(row["user_id"]!=null)
				{
					model.user_id=row["user_id"].ToString();
				}
				if(row["port"]!=null)
				{
					model.port=row["port"].ToString();
				}
				if(row["is_red"]!=null && row["is_red"].ToString()!="")
				{
					model.is_red=int.Parse(row["is_red"].ToString());
				}
				if(row["sort_id"]!=null && row["sort_id"].ToString()!="")
				{
					model.sort_id=int.Parse(row["sort_id"].ToString());
				}
				if(row["id_type"]!=null)
				{
					model.id_type=row["id_type"].ToString();
				}
				if(row["group_id"]!=null && row["group_id"].ToString()!="")
				{
					model.group_id=int.Parse(row["group_id"].ToString());
				}
				if(row["pos_mark"]!=null)
				{
					model.pos_mark=row["pos_mark"].ToString();
				}
				if(row["attr_str"]!=null)
				{
					model.attr_str=row["attr_str"].ToString();
				}
				if(row["sttr_str2"]!=null)
				{
					model.sttr_str2=row["sttr_str2"].ToString();
				}
				if(row["attr_num"]!=null && row["attr_num"].ToString()!="")
				{
					model.attr_num=int.Parse(row["attr_num"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,title,user_id,port,is_red,sort_id,id_type,group_id,pos_mark,attr_str,sttr_str2,attr_num ");
			strSql.Append(" FROM he_olservice ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQLite.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM he_olservice ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            object obj = DbHelperSQLite.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from he_olservice T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQLite.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@tblName", DbType.VarChar, 255),
					new SQLiteParameter("@fldName", DbType.VarChar, 255),
					new SQLiteParameter("@PageSize", DbType.Int32),
					new SQLiteParameter("@PageIndex", DbType.Int32),
					new SQLiteParameter("@IsReCount", DbType.bit),
					new SQLiteParameter("@OrderType", DbType.bit),
					new SQLiteParameter("@strWhere", DbType.VarChar,1000),
					};
			parameters[0].Value = "he_olservice";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

